package com.teckteam.ptvsystem.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.teckteam.ptvsystem.controller.CancelWindowListener;
import com.teckteam.ptvsystem.model.AbstractStop;
import com.teckteam.ptvsystem.model.facade.PTVConstants;
import com.teckteam.ptvsystem.model.facade.PTVModel;
import com.teckteam.ptvsystem.model.facade.PTVSystem;

/**
 * Dialog prompt for easier selection of stops from the list.
 * (Search by suburb or transport)
 */
public class StopSearchPrompt extends JDialog
{
   private JourneyPlanner jpDialog;
   private JComboBox cmBox;
   private ButtonGroup btnGroup;
   private JRadioButton
      trainBtn, tramBtn, busBtn;
   private JButton
      okBtn, cancelBtn;
   private JTabbedPane tabPane = new JTabbedPane();
   private JPanel
      dialogPanel, btnPanel;
   private PTVModel model = PTVSystem.getInstance();
   private ArrayList<String> 
      trainList = model.getAllGroupedRoutes(PTVConstants.TRAIN_FLAG),
      tramList = model.getAllGroupedRoutes(PTVConstants.TRAM_FLAG),
      busList = model.getAllGroupedRoutes(PTVConstants.BUS_FLAG);
   private JList subListBox;
   
   public StopSearchPrompt(JourneyPlanner jpDialog, final JourneyChoicePanel journeyChoicePanel)
   {
      ArrayList<String> 
         trainStopList = new ArrayList<String>(),
         tramStopList = new ArrayList<String>(),
         busStopList = new ArrayList<String>(),
         suburbList = jpDialog.getSuburbList();
         
      this.jpDialog = jpDialog;
      cmBox = new JComboBox();
      btnGroup = new ButtonGroup();
      trainBtn = new JRadioButton("Train");
      tramBtn = new JRadioButton("Tram");
      busBtn = new JRadioButton("Bus");
      btnGroup.add(trainBtn);
      btnGroup.add(tramBtn);
      btnGroup.add(busBtn);
      
      dialogPanel = new JPanel(new BorderLayout());
      btnPanel = new JPanel();
      okBtn = new JButton("OK");
      cancelBtn = new JButton("Cancel");
      btnPanel.add(okBtn);
      btnPanel.add(cancelBtn);
      
      setModal(true);
      setTitle("Stop search");
      setLocationRelativeTo(null);
      setSize(300,300);
      
      okBtn.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
            System.out.printf("TBI: process tab %d\n", tabPane.getSelectedIndex());
            switch(tabPane.getSelectedIndex())
            {
               case 0:
                  ArrayList<AbstractStop> stops = model.getAllSuburbStops(subListBox.getSelectedValue().toString());
                  DefaultListModel chooseList = new DefaultListModel();
                  for(AbstractStop s : stops)
                  {
                     System.out.println(s.toString());
                     chooseList.addElement(s.toString());
                  }
                  final JList popupList = new JList(chooseList);
                  final JDialog foundListDialog = new JDialog();
                  foundListDialog.setModal(true);
                  JPanel popupPanel = new JPanel(new BorderLayout());
                  JScrollPane popupPane = new JScrollPane(popupList);
                  popupList.addListSelectionListener(new ListSelectionListener() 
                  {
                     public void valueChanged(ListSelectionEvent e)
                     {
                        System.out.println("Selected" + popupList.getSelectedValue().toString());
                        journeyChoicePanel.setStop(popupList.getSelectedValue().toString());
                        foundListDialog.setVisible(false);
                     }
                  });
                  foundListDialog.getContentPane().add(popupPane, BorderLayout.CENTER);
                  foundListDialog.setSize(200,500);
                  foundListDialog.setLocationRelativeTo(null);
                  StopSearchPrompt.this.setVisible(false);
                  foundListDialog.setVisible(true);
                  break;
               case 1:
                  System.out.println("NOT IMPLEMENTED");
                  break;
            }
         }
      });
      
      cancelBtn.addActionListener(new CancelWindowListener(this));
      
      Collections.sort(suburbList);
      
      //TODO
      
      //Search by Suburb
      JPanel suburbTab = new JPanel();
      DefaultListModel subListModel = new DefaultListModel();
      for(String s:suburbList)
         subListModel.addElement(s);
      
      subListBox = new JList(subListModel);
      suburbTab.add(subListBox);
      
      //Search by Transport (Stop)
      JPanel stopTab = new JPanel();
      DefaultListModel
         trainStopListModel = new DefaultListModel(),
         tramStopListModel = new DefaultListModel(),
         busStopListModel = new DefaultListModel();
      
      for(String s:trainStopList)
         trainStopListModel.addElement(s);
      
      for(String s:tramStopList)
         tramStopListModel.addElement(s);
      
      for(String s:busStopList)
         busStopListModel.addElement(s);
      
      //TODO
      JList stopListBox = new JList(trainStopListModel); 
      stopTab.add(trainBtn);
      stopTab.add(tramBtn);
      stopTab.add(busBtn);
      stopTab.add(stopListBox);
      
      //Search by Transport (Route)
      tabPane.addTab("Search by Suburb", new JScrollPane(subListBox));
      tabPane.addTab("Search by Transport", new JScrollPane(stopListBox));
      
      add(tabPane, BorderLayout.CENTER);
      add(btnPanel, BorderLayout.SOUTH);
      setVisible(true);
   }
}
